<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./js/vue.js"></script>

    <style>
        * {
            padding: 0;
            margin: 0;
        }
        
        table {
            border: 1px solid #e9e9e9;
            border-collapse: collapse;
            border-spacing: 0;
        }
        
        th,
        td {
            padding: 8px 16px;
            border: 1px solid #e9e9e9;
            text-align: left;
        }
        
        th {
            background-color: #f7f7f7;
            color: #5c6b77;
            font-weight: 600;
        }
    </style>
</head>

<body>
    <div id="app">
        <div v-if="list.length">
            <table>
                <thead>
                    <tr>
                        <th></th>
                        <th>书籍名称</th>
                        <th>出版日期</th>
                        <th>价格</th>
                        <th>购买数量</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="(item, index) in list">
                        <td>{{item.id}}</td>
                        <td>{{item.name}}</td>
                        <td>{{item.date}}</td>
                        <td>{{showPrice(item.price) }}</td>
                        <td>
                            <button @click="decrement(index)" v-bind:disabled="item.count<=1">-</button> {{item.count}}
                            <button @click="increment(index)">+</button>
                        </td>
                        <td><button @click="removeHandler">移除</button></td>
                    </tr>
                </tbody>
            </table>
            <h2>总价格：{{showPrice(totalPrice) }}</h2>
        </div>
        <h2 v-else>购物车被清空</h2>
    </div>

    <script>
        new Vue({
            el: '#app',
            data: {
                list: [{
                    id: 1,
                    name: '《斗罗大陆》',
                    date: '2006-9',
                    price: 10.00,
                    count: 1
                }, {
                    id: 2,
                    name: '《三国演义》',
                    date: '2006-2',
                    price: 30.00,
                    count: 1
                }, {
                    id: 3,
                    name: '《西游记》',
                    date: '2008-10',
                    price: 20.00,
                    count: 1
                }, {
                    id: 4,
                    name: '《水浒传》',
                    date: '2006-3',
                    price: 90.00,
                    count: 1
                }],
            },
            methods: {
                increment(index) {
                    this.list[index].count++
                },
                decrement(index) {
                    this.list[index].count--
                },
                removeHandler(index) {
                    this.list.splice(index, 1) //splice( 下标 ， 删除第一个 )  从下标的位置删除一个
                },
                showPrice(price) {
                    return '￥' + price.toFixed(2)
                }
            },
            computed: {
                totalPrice() { //  计算总数
                    let totalPrice = 0
                    for (let i = 0; i < this.list.length; i++) {
                        totalPrice += this.list[i].price * this.list[i].count
                    }
                    return totalPrice
                }
            },
            // filters: {
            //     showPrice(price) {
            //         return '￥' + price.toFixed(2)
            //     }
            // }
        })
    </script>
</body>

</html>